/**
 * 
 */
package it.battlehorse.scripting.mapping;

import it.battlehorse.scripting.IScriptingConstants;
import it.battlehorse.scripting.registry.IScriptingRegistry;
import it.battlehorse.scripting.registry.impl.BasicScriptingRegistry;

import org.eclipse.core.runtime.IConfigurationElement;


/**
 * Performs the mapping between a <code>scriptedActionSet</code> and a traditional eclipse <code>actionSet</code>.
 * @author battlehorse
 *
 */
public class ActionContributionMapper implements IContributionMapper, IScriptingConstants {

	/* (non-Javadoc)
	 * @see it.battlehorse.scripting.mapping.IContributionMapper#getContribution(org.eclipse.core.runtime.IConfigurationElement)
	 */
	public String getContribution(IConfigurationElement el) {
		
    	StringBuilder sb = new StringBuilder();

    	sb.append("<plugin>");
    	sb.append("<extension point=\"org.eclipse.ui.actionSets\">");
    	sb.append("<actionSet ");

    	// id attribute
    	sb.append(" id=\"").append(getContributionId(el)).append("\" ");   
    	appendAttribute(sb, el, "label");
    	appendAttribute(sb, el, "visible");
    	sb.append(" > ");
    	
    	for (IConfigurationElement actionEl : el.getChildren(EL_SCRIPTED_ACTION)) {
        	sb.append("<action ");
        	appendAttribute(sb, actionEl, "accelerator");
        	appendAttribute(sb, actionEl, "label");
        	appendAttribute(sb, actionEl, "icon");
        	
        	// default attributes 
        	sb.append(" enablesFor=\"*\" ");
        	sb.append(" menubarPath=\"it.battlehorse.rcp.tools.actions/it.battlehorse.scripting.scriptactionsmenu/autoActions\" ");
        	sb.append(" style=\"push\" ");
        	sb.append(" retarget=\"false\" ");
        	
        	// id attribute
        	sb.append(" id=\"").append(getActionContributionId(actionEl)).append("\" ");
        	
        	// Class attribute
        	sb.append(" class=\"it.battlehorse.scripting.actions.ScriptProxyAction\"");    	    	
        	
        	sb.append("/>");    		
    	}
    	
    	sb.append("</actionSet>");
    	sb.append("</extension>");
    	sb.append("</plugin>");      	
    	
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.scripting.mapping.IContributionMapper#getContributionId(org.eclipse.core.runtime.IConfigurationElement)
	 */
	public String getContributionId(IConfigurationElement el) {
		return el.getAttribute("id") + ".autoActionSet";
	}
	
	private String getActionContributionId(IConfigurationElement el) {
		return el.getAttribute("id") + ".autoAction";
	}

	/* (non-Javadoc)
	 * @see it.battlehorse.scripting.mapping.IContributionMapper#updateScriptingRegistry(it.battlehorse.scripting.registry.IScriptingRegistry, org.eclipse.core.runtime.IConfigurationElement)
	 */
	public void updateScriptingRegistry(IScriptingRegistry registry,IConfigurationElement el) {
		
		BasicScriptingRegistry basicReg = (BasicScriptingRegistry) registry;
		
		for (IConfigurationElement actionEl : el.getChildren(EL_SCRIPTED_ACTION)) {
			IConfigurationElement scriptElement = actionEl.getChildren(EL_SCRIPT)[0];
			basicReg.put(
					getActionContributionId(actionEl), 
					scriptElement.getAttribute("id"), 
					scriptElement.getAttribute("uri"), 
					scriptElement.getAttribute("extension"),
					actionEl.getNamespaceIdentifier(),
					"true".equals(scriptElement.getAttribute("modal")));
			
		}
		
	}
	
	private void appendAttribute(StringBuilder sb, IConfigurationElement el, String attribute) {
		if (el.getAttribute(attribute) != null)
			sb.append(" ").append(attribute).append("=\"").append(el.getAttribute(attribute)).append("\" ");
	}	

}
